﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SupportLibrary;
using System.Diagnostics;
using System.IO;
using SupportLibrary.Config;

namespace MeGate
{
//    //CALL WS AND JMS FROM COMMAND LINE
//java -cp TestMEGate.jar com.yellowpepper.megate.test.TestGateClient [args1] [args2] [args3] [args4]
//[args1] ws | jms : call ws services or jms services
//[args2] url (refer example)
//[args3] filename of file which containt request/response data
//[args4] the number of running process
////CALL WS EXAMPLE
//java -cp TestMEGate.jar com.yellowpepper.megate.test.TestGateClient ws http://220.231.95.199:8888/MEGateService/Services NewFile.xml 3

////CALL JMS EXAMPLE

//java -cp TestMEGate.jar com.yellowpepper.megate.test.TestGateClient jms http://192.168.80.90:8888/me-gate-web/simulation NewFile.xml 3

    public class TestMegate
    {
        private Dictionary<string,string> _tconfig;

        public TestMegate(Dictionary<string, string> config)
        { 
            _tconfig = config;

        }


        private void InvokeJavaApp(string arguments)
        {
            try
            {
                string mepath = string.Empty;
                string javapath = string.Empty;


                //mepath = "D:\\Working\\Performance test\\MECore\\VCClass";
                //javapath = "C:\\temp\\jre6\\bin\\java.exe";
                mepath = ConfManager.DefaultObjectPath;
                javapath = "java.exe";
                string result = string.Empty;

                Process process = new Process();

                process.EnableRaisingEvents = false;
                process.StartInfo.FileName = javapath;
                process.StartInfo.WorkingDirectory = mepath;


                process.StartInfo.Arguments = arguments;

                process.StartInfo.CreateNoWindow = false;
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                process.StartInfo.RedirectStandardOutput = true;

                process.Start();
                using (StreamReader reader = process.StandardOutput)
                {
                    result = reader.ReadToEnd();
                    Console.WriteLine(result);
                }

                


            }
            catch (Exception ex)
            {
            
               // throw AutoException(
                throw new AutoException(Errors.InvokeJavaApp);
            }
        }


        [Automation]
        public void TestGateClient(string service, string urlservice, string filename, string numberofrun)
        {
            string arguments = "-cp TestMEGate.jar com.yellowpepper.megate.test.TestGateClient ";
            try
            {
               if(service =="ws")
               {
                   arguments = arguments + "ws "; 
               }
                else{
                    arguments = arguments + "jms "; 
               }

               arguments = arguments + urlservice + " " + filename + " " + numberofrun;

                Console.WriteLine("Start:" + arguments + DateTime.Now.ToString());
                InvokeJavaApp(arguments);
                Console.WriteLine("Stop:" + DateTime.Now.ToString());

            }
            catch (Exception ex)
            {
                throw new AutoException(Errors.InvovkeMegateFuntion);
            }
            
        }
    }
}
